package org.example.server;

import java.io.IOException;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

public class HttpServer03 {

    public static void main(String[] args) throws IOException {
        final int port = 8803;
        final int availableProcessors = Runtime.getRuntime().availableProcessors();
        final int nThreads = availableProcessors + 2;
        ExecutorService executorService = Executors.newFixedThreadPool(nThreads);
        final HttpServerCommon httpServerCommon = new HttpServerCommon("hello,nio3");
        ServerSocket serverSocket = new ServerSocket(port);
        System.out.println("Listening: http://127.0.0.1:" + port + ", thread pool size=" + nThreads);
        while (!Thread.currentThread().isInterrupted()) {
            try {
                Socket s = serverSocket.accept();
                executorService.execute(() -> httpServerCommon.service(s));
            } catch (IOException ex) {
                ex.printStackTrace();
            }
        }
    }
}
